package com.wpml.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wpml.common.util.PageUtils;
import com.wpml.common.util.Query;
import com.wpml.product.dao.ProductAttrValueDao;
import com.wpml.product.entity.ProductAttrValueModel;
import com.wpml.product.service.ProductAttrValueService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * spu属性值(PmsProductAttrValue)表服务实现类
 *
 * @author makejava
 * @since 2020-04-07 11:33:36
 */
@Service
@Slf4j
public class ProductAttrValueServiceImpl extends ServiceImpl<ProductAttrValueDao, ProductAttrValueModel> implements ProductAttrValueService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ProductAttrValueModel> page = this.page(new Query<ProductAttrValueModel>().getPage(params), new QueryWrapper<>());

        return new PageUtils(page);
    }

    @Override
    public void saveProductAttr(List<ProductAttrValueModel> collect) {
        this.saveBatch(collect);
    }

    @Override
    public List<ProductAttrValueModel> baseAttrlistforspu(Long spuId) {
        return this.baseMapper.selectList(new QueryWrapper<ProductAttrValueModel>().eq("spu_id", spuId));
    }

    @Transactional
    @Override
    public void updateSpuAttr(Long spuId, List<ProductAttrValueModel> entities) {
        //1、删除这个spuId之前对应的所有属性
        this.baseMapper.delete(new QueryWrapper<ProductAttrValueModel>().eq("spu_id", spuId));


        List<ProductAttrValueModel> collect = entities.stream().map(item -> {
            item.setSpuId(spuId);
            return item;
        }).collect(Collectors.toList());
        this.saveBatch(collect);
    }
}
